package com.example.carbon.server.info;

import com.alibaba.fastjson2.JSON;
import com.example.carbon.dao.BomManagerDao;
import com.example.carbon.dao.BomMaterialDao;
import com.example.carbon.pojo.dto.BomManagerDto;
import com.example.carbon.pojo.dto.PageLimit;
import com.example.carbon.pojo.vo.TableData;
import com.example.carbon.server.BomManagerServerInfo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class BomManagerServer implements BomManagerServerInfo {

    @Resource
    BomManagerDao bomManagerDao;//bom本身管理的dao

    @Resource
    BomMaterialDao bomMaterialDao;//bom与物料关系的dao

    /**
     * 获取bom数据
     *
     * @param pageLimit
     * @param name
     * @return
     */
    @Override
    public String selectAllInfo(PageLimit pageLimit, String name) {
        try {
            TableData tableData = new TableData<>(bomManagerDao.selectAllInfo(pageLimit, name),
                    bomManagerDao.countOfSelect(name));
            return JSON.toJSONString(tableData);
        } catch (Exception e) {
            log.info(e.getMessage());
            return null;
        }
    }

    /**
     * 删除bom数据
     * @param id
     * @return
     */
    @Override
    public boolean delInfoById(int id) {
        try {
            return bomManagerDao.delById(id);
        } catch (Exception e) {
            log.info(e.getMessage());
            return false;
        }
    }

    /**
     * 插入一条新的bom数据
     * @param bomManagerDto
     * @return
     */
    @Override
    public boolean insertNewInfo(BomManagerDto bomManagerDto) {
        try {
            return bomManagerDao.insertNewInfo(bomManagerDto);
        } catch (Exception e) {
            log.info(e.getMessage());
            return false;
        }
    }

    /**
     * 更新关系，以及更新bom的信息
     * @param bomManagerDto
     * @return
     */
    @Override
    public boolean updateNewInfo(BomManagerDto bomManagerDto) {
        try {
            if (bomManagerDto.getBom_rel_id().size() > 0) {
                bomMaterialDao.delInfo(bomManagerDto.getId());//先进性删除
                for (String s : bomManagerDto.getBom_rel_id()) {
                    bomMaterialDao.insertNewInfo(bomManagerDto.getId(), s);//在进行逐步添加
                }
            }
            return bomManagerDao.updateNewInfo(bomManagerDto, Integer.parseInt(bomManagerDto.getId()));
        } catch (Exception e) {
            log.info(e.getMessage());
            return false;
        }
    }

    /**
     * 通过bom的id来查找相关联的物料档案数据
     * @param id
     * @return
     */
    @Override
    public String selectRelByBomId(int id) {
        try {
            return JSON.toJSONString(bomManagerDao.selectByBomId(id));
        }catch (Exception e){
            log.info(e.getMessage());
            return null;
        }

    }

    @Override
    public String selectBomInfoById(int id) {
        try {
            return JSON.toJSONString(bomManagerDao.selectBomManagerById(id));
        }catch (Exception e){
            return null;
        }

    }


}
